package com.sise.netty.reactor模型.多线程模型;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.concurrent.*;

/**
 * 专门处理线程任务
 */
public class Processor {

    private static final ExecutorService service =new ThreadPoolExecutor(16, 16,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());


    public void process(SelectionKey selectionKey) {
        service.submit(() -> {
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
            int count = socketChannel.read(buffer);
            if (count < 0) {
                socketChannel.close();
                selectionKey.cancel();
                System.out.println(" Read ended");
                return null;
            } else if (count == 0) {
                return null;
            }
            System.out.println("Read message {}");
            return null;
        });
    }
}